<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>

  <body>
    <div id="app">
      <div>
        <span>姓名:</span>
        <input type="text" v-model="name" />
      </div>
      <div>
        <span>年龄:</span>
        <input type="number" v-model="age" />
      </div>
      <div>
        <span>性别:</span>
        <select>
          <option value="男" v-model="sex">男</option>
          <option value="女" v-model="sex">女</option>
        </select>
      </div>
      <div>
        <button @click="add">添加/修改</button>
      </div>
      <div>
        <table border="1" cellpadding="10" cellspacing="0">
          <tr>
            <th>序号</th>
            <th>姓名</th>
            <th>年龄</th>
            <th>性别</th>
            <th>操作</th>
          </tr>
          <tr v-for="(item,index) in list" :key="index">
            <td>{{ index+1 }}</td>
            <td>{{ item.name }}</td>
            <td>{{ item.age }}</td>
            <td>{{ item.sex }}</td>
            <td>
              <button @click="del(index)">删除</button>
              <button @click="edit(index)">编辑</button>
            </td>
          </tr>
        </table>
      </div>
    </div>
    <script src="../vue.js"></script>
    <script>
      const app = new Vue({
        el: "#app",
        data: {
          list: [
            { name: "张三", age: 18, sex: "男" },
            { name: "李四", age: 19, sex: "女" },
            { name: "王五", age: 20, sex: "男" },
            { name: "赵六", age: 21, sex: "女" },
            { name: "钱七", age: 22, sex: "男" },
            { name: "孙八", age: 23, sex: "女" },
          ],
          name: "",
          age: "",
          sex: "男",
          YN: false,
          indexx: 0,
        },
        methods: {
          add() {
            if (this.YN) {
              this.YN = false;
              this.list[this.indexx].name = this.name;
              this.list[this.indexx].age = this.age;
              this.list[this.indexx].sex = this.sex;
            } else {
              if (this.name == "") {
                alert("请输入姓名");
                return;
              }
              if (this.age == "") {
                alert("请输入年龄");
                return;
              }
              this.list.push({
                name: this.name,
                age: this.age,
                sex: this.sex,
              });
            }
            this.name = "";
            this.age = "";
            this.sex = "男";
          },
          edit(index) {
            this.name = this.list[index].name;
            this.age = this.list[index].age;
            this.sex = this.list[index].sex;
            this.YN = true;
            this.indexx = index;
          },
          del(index) {
            this.list = this.list.filter((item, i) => i !== index);
          },
        },
      });
    </script>
  </body>
</html>
